package com.nbot.server.command;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.nbot.common.AbstractInterpreter;
import com.nbot.common.command.BaseCommand;
import com.nbot.common.command.b2s.Command;
import com.nbot.common.command.s2b.SelectBackCommand;
import com.nbot.common.domain.SearchResult;
import com.nbot.server.BotServer;
import com.nbot.server.domain.Point;
import com.nbot.server.properties.NbotProperties;
import com.nbot.server.util.NbotStringUtil;
import com.nbot.server.util.SpringUtil;
import com.nbot.server.workers.DbWorker;
import com.nbot.server.workers.OpcWorker;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class CommandInterpreter extends AbstractInterpreter {
    private static final Log log = LogFactory.get();
    public static final String NOTFOUND_STRING = "查询不到此值";

    public CommandInterpreter(BaseCommand baseCommand) {
        super(baseCommand);
    }

    @Override
    public void execute() {
        Command selectCommand = (Command) this.getBaseCommand();
        DbWorker dbWorker = (DbWorker) SpringUtil.getBean("dbWorker");
        OpcWorker opcWorker = (OpcWorker) SpringUtil.getBean("opcWorker");
        // PointService pointService = (PointService) SpringUtil.getBean("pointService");
        String searchDesc = StrUtil.cleanBlank(selectCommand.getOpcKey().toUpperCase());

        searchDesc = NbotStringUtil.convertHanziToShuzi(searchDesc);
        String result = NOTFOUND_STRING;
        //从本地数据库通过描述字符串查询key
        Point point = dbWorker.getPointFormDesc(searchDesc);
        List<Point> points = null;
        String tmp = "";
        SelectBackCommand back = null;
        if (point != null) {
            try {
                result = opcWorker.readData(point.getTag());
                result = NbotStringUtil.setScale(result, 2);
                tmp = point.getDispdesc();
                back = new SelectBackCommand(tmp, result);

            } catch (Exception e) {
                e.printStackTrace();
            }
            points = dbWorker.getSaveWeizhiPoint(point.getWeizhi());
            List<SearchResult> relatedPoint = new ArrayList<>();
            if (points != null) {
                System.err.println(points.remove(point));
                for (Point p : points) {
                    try {
                        result = opcWorker.readData(p.getTag());
                        result = NbotStringUtil.setScale(result, 2);
                        tmp = p.getDispdesc();
                        relatedPoint.add(new SearchResult(tmp, result));
//                        SelectBackCommand back = new SelectBackCommand(tmp, result);
//                        BotServer.putCommand(back);
                        back.setRelatedPoint(relatedPoint);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

//            BotServer.putCommand(back);
            try {
                BotServer.addUartMsg(back);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        } else {
            //log.debug("SERVER执行opc查询[" + tmp + "的结果是]:" + result);
            back = new SelectBackCommand(searchDesc, result);
//            BotServer.putCommand(back);
            try {
                BotServer.addUartMsg(back);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }


    }

}
